package com.example.enginetestandroid.manager;

import android.content.Context;

import com.example.enginetestandroid.App;

import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;

public class AppFlutterEngineManager {

    public static FlutterEngine flutterEngine(Context context, String engineId, String entryPoint) {
        // 1. 从缓存中获取FlutterEngine
        FlutterEngine engine = FlutterEngineCache.getInstance().get(engineId);
        if (engine == null) {
            // 如果缓存中没有FlutterEngine
            // 2. 新建FlutterEngine，执行的入口函数是entryPoint
            DartExecutor.DartEntrypoint dartEntrypoint = new DartExecutor.DartEntrypoint(FlutterInjector.instance().flutterLoader().findAppBundlePath(), entryPoint);
            engine = App.flutterEngineGroup.createAndRunEngine(context, dartEntrypoint);
            // 3. 存入缓存
            FlutterEngineCache.getInstance().put(engineId, engine);
        }
        return engine;
    }
}
